<?php
	$ls_gridname	= 'dtl_sample2';


// ----------------- >> Form Detail << ----------------- ?>
<script type="text/javascript">

	$(document).ready(function(){
		// Setup Form

		$('#form_<?php echo $ls_gridname; ?>').setupForm([
			//{ name: 'id_perakitan_barang_detail', type: 'text', size: 'medium', maxlength: 200, readonly:true },		
			{ name: 'kode_produk', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#banyak').setval(''); 
				},
				onActionButton:function(){ 
					LOVInventori3_xx({ param:'?id_perakitan_barang='+$('#id_perakitan_barang').getval(), onSelect:function(row){ 
						$('#kode_produk').setval(row.kode_inventori); 
						$('#nama_inventori').setval(row.nama_inventori);
						$('#id_inventori').setval(row.id_inventori); 
						$('#stock').setval(row.banyak);  
						$('#banyak').setval(1); 
					} }); 
				}
			},
			{ name: 'banyak', type: 'text', size: 'medium', maxlength: 200},
			{ name: 'stock', type: 'text', size: 'medium', maxlength: 200, readonly:true },
			{ name: 'nama_inventori', type: 'text', size: 'medium', maxlength: 200, readonly:true },
		]);		

		$('#form_<?php echo $ls_gridname; ?>_container').dialog({
			closed:true,
			modal:true,
			title: 'Tambah Detail',
			width:400,
			height:200,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					var ls_mode			= $('#form_<?php echo $ls_gridname; ?>').data('mode');
					var ls_error		= '';
					var row	= formToJSON('form_<?php echo $ls_gridname; ?>');
					console.log(row);
					// $('#form_master_detail').submitForm('/'+controller+'/ajax_post_detail', { onComplete : function(){ $('#dialog_container_detail').dialog('close'); $('#datagrid_detail').datagrid('reload'); }} );
					
					if(row.kode_produk == ''){
						ls_error	+= 'Isi Barang\n';
					}
					if(row.banyak == 0){
						ls_error	+= 'Isi Jumlah Barang\n';
					}
					
					// Eksekusi
					if(ls_error!=''){
						$.messager.alert('Error', nl2br(ls_error), 'error');
					}else{
						if(ls_mode=='create'){
							
							$('#<?php echo $ls_gridname; ?>').datagrid('appendRow', row );
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');	
						
						} else {
							var selected = $('#<?php echo $ls_gridname; ?>').datagrid('getSelected');
							var rowIndex = $("#<?php echo $ls_gridname; ?>").datagrid("getRowIndex", selected);

							updateRowFlag('<?php echo $ls_gridname; ?>', row); // Patching Flag Update
							$('#<?php echo $ls_gridname; ?>').datagrid('updateRow',{index:rowIndex,row:row});
							
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
						}
					}
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
				}
			}]
		});

	});
</script>


<div id="form_<?php echo $ls_gridname; ?>_container" style="padding:5px;width:800px;height:400px;">
	<div id="form_<?php echo $ls_gridname; ?>">
    
    <table class="tb_form_detail" style="width:100%;" cellpadding="0" cellspacing="0">    
    <tr>
        <td>
        	<input id="id_perakitan_barang_detail" name="id_perakitan_barang_detail" type="hidden"/>
        	<input id="id_inventori" name="id_inventori" type="hidden"/>
        </td>
    </tr>
    <tr>
        <td class="label">Kode Produk</td>
        <td><input id="kode_produk" /></td>
    </tr>
    <tr>
        <td class="label">Nama Produk</td>
        <td><input id="nama_inventori" /></td>
    </tr>
    <tr>
        <td class="label">Stock</td>
        <td><input id="stock" /></td>
    </tr>
    <tr>
        <td class="label">Banyak Produk</td>
        <td><input id="banyak" /></td>
    </tr>
    </table>
	</div>
</div>

<?php 

// ----------------- >> Table Detail << ----------------- ?>
<script type="text/javascript">
	$(document).ready(function(){

		/*var id_perakitan_detail = '<?php echo $id_perakitan_detail; ?>'*/

		// var jrow = $('#<?php echo $ls_gridname; ?>').datagrid('getRows').length;
		// if(jrow != 0){
		// 	for(var i=0; i<jrow; i++){
		// 		$('#<?php echo $ls_gridname; ?>').datagrid('deleteRow',i);
		// 	}
		// }

		$('#<?php echo $ls_gridname; ?>').datagrid({
			url:gs_path+'/'+controller+'/<?php echo $ls_gridname; ?>_data',
			queryParams:{ id_perakitan_barang:$('#id_perakitan_barang').getval(), rows:9999999999 },
			<?php if($mode!='view'){ 
			// -->> Toolbar [Start]
			// Only Appear On Mode Create / Edit ?>
			toolbar:[{
				text:'Add',
				iconCls:'icon-add',
				handler:function(){

					$.ajax  ({
						    	url: gs_path+'/'+controller+'/IdDetail/',
						    	type: 'POST',
						    	dataType: 'json',
						    	data: {option : null},
						    })
						    .done(function(data){ 
								var id_perakitan_detail = data['id_perakitan_detail'];
								$('#id_perakitan_barang_detail').setval(id_perakitan_detail);
							});
						resetError();
						$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
						$('#form_<?php echo $ls_gridname; ?>').data('mode', 'create');
						$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Tambah Detail Perakitan');					
						$('#form_<?php echo $ls_gridname; ?>').resetForm();
						
					
				}
			},'-',{
				text:'Edit',
				iconCls:'icon-edit',
				handler:function(){
					var oSel=$('#<?php echo $ls_gridname; ?>').datagrid('getSelected'); 
					if(oSel==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else{ 
						resetError();
						//alert(oSel['id_perakitan_barang_detail']);
						$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
						$('#form_<?php echo $ls_gridname; ?>').data('mode', 'edit');
						$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Ubah Detail Barang');
						$('#id_perakitan_barang_detail').setval(oSel['id_perakitan_barang_detail']);
						jsonToForm(oSel);
					}
				}
			},'-',{
				text:'Delete',
				iconCls:'icon-remove',
				handler:function(){
					var row = $('#<?php echo $ls_gridname; ?>').datagrid('getSelected');
					if(row==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else if(row){
						if(!confirm('Hapus baris data?')) return false;

						var index = $('#<?php echo $ls_gridname; ?>').datagrid('getRowIndex', row);
						$('#<?php echo $ls_gridname; ?>').datagrid('deleteRow', index);
					}
				}
			}],<?php 
			} // -->> Toolbar [End]
			?>
			onClickRow:function(rowIndex){
				var lastIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
				$('#<?php echo $ls_gridname; ?>').data('selectedIndex', rowIndex);
			},
			onLoadSuccess:function(){ }
		});
		
	});
	
</script>


<table id="<?php echo $ls_gridname; ?>" rownumbers="true" style="width:600;height:auto" singleSelect="true" idField="#" showFooter="true">
<thead>
    <tr>
    	<th field="id_perakitan_barang_detail" width="200">ID Produk Detail</th>
    	<th field="kode_produk" width="200">Kode Produk</th>
        <th field="nama_inventori" width="200">Nama Produk</th>
        <th field="stock" width="120" align="right">Stock</th>
        <th field="banyak" width="120" align="right">Banyak Produk</th>
        <th field="id_inventori" width="120" align="right">Id Inventori</th>
    </tr>
</thead>
</table>
